import {messages} from "share/common";
/**
 * 操作：确认付款
 * 适用：
 */
import React from 'react'
import { connect } from 'react-redux'
import menuRoute from 'routes/menuRoute'
import { Form, Button, Icon } from 'antd'
import {notification} from "antd/lib/index";
import confirmPaymentService from "./confirm-payment.service";

class SurePayBtn extends React.Component{
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      applicationList: menuRoute.getRouteItem('request','key'), //申请单列表页

    }
  }
  //点击确认付款
  handleConfirmPay = () => {
    this.setState({loading: true}, () => {
      let {applicantInfo} = this.props;
      let params = {
        comment: null,
        endDate: null,
        entityOIDs: [applicantInfo.applicationOID],
        entityType: 1002,
        excludedEntityOIDs: [],
        formOIDs: [],
        selectMode: 'default',
        startDate: null,
        status: 'pay_in_process',
        businessCode: applicantInfo.businessCode,
        applicantOID: applicantInfo.applicantOID
      }
      confirmPaymentService.confirmPayment('processing', params).then(() => {
        this.setState({submitting: false});
        notification.open({
          message: messages('financial-management.key179'/*确认付款成功*/),
          // description: `您有1笔单据确认付款成功:)`,
          description: messages('financial-management.key546',{arg1: 1}/*{arg1}笔单据确认付款成功*/),
          icon: <Icon type="smile-circle" className='default-color-font'/>,
        });
        this.goBack()
      }).catch((e) => {
        this.setState({submitting: false});
        notification.open({
          message: messages('financial-management.key151'/*确认付款失败*/),
          description: messages('common.error'/*可能是服务器出了点问题:(*/),
          icon: <Icon type="frown-circle" className='default-color-inverse-font'/>,
        });
      })
    });

  };
  //返回
  goBack = () => {
    if(this.props.backType==='history')
    {
      window.history.go(-1);
    }
    else {
      this.context.router.push(this.state.applicationList.url)
    }


  };

  render() {
    const { loading } =this.state;
    return (
      <div className="go-back-btn">
        {!this.props.onlyBack && (
          <Button
            type="primary" className="back-btn" onClick={this.handleConfirmPay} style={{marginRight:20}}
            loading={loading}>{messages('financial-management.key180'/*确认已付款*/)}</Button>
        )}
        <Button onClick={this.goBack}>{messages('common.back')}</Button>
      </div>
    )
  }
}

SurePayBtn.contextTypes = {
  router: React.PropTypes.object
};

function mapStateToProps() {
  return { }
}

const wrappedGoBackBtn = Form.create()(SurePayBtn);

export default connect(mapStateToProps)(wrappedGoBackBtn)
